import { EResType } from "../../Common/Enum";
import EventID from "../../Common/EventID";
import Message from "../../Common/Message";
import StorageUtil from "../../Util/StorageUtil";
import { AdManager } from "../ads/AdManager";
import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM, GAME_TYPE_ENUM, RELEASE_TYPE_ENUM } from "../base/EnumIndex";
import { Tools } from "../common/Tools";
import { AudioManager1 } from "../manager/AudioManager1";
import { EventManager } from "../manager/EventManager";
import { GameManager } from "../manager/GameManager";
import { LevelManager } from "../manager/LevelManager";
import { UIManager } from "../manager/UIManager";
import { LvInfo } from "../model/OtherModel";

const { ccclass, property } = cc._decorator;

@ccclass
export default class WinUI extends cc.Component {
    // @property( { type:cc.Node , displayName:"本关卡胜利图片" } )
    // LvSpr:cc.Node = null;
    @property({ type: cc.Label, displayName: "胜利信息" })
    winLab: cc.Label = null;

    @property({ type: cc.Node, displayName: "下一关" })
    nextBtn: cc.Node = null;
    @property({ type: cc.Node, displayName: "分享" })
    shareBtn: cc.Node = null;
    @property(cc.Node)
    addBtn: cc.Node = null;

    nextLvInfo: LvInfo;

    protected onLoad(): void {
        this.ceshiWinPassLv();
        AudioManager1.stopAllAudio();
        AudioManager1.playAudio("chenggong", 1, BUNDLE_TYPE_ENUM.GAME);

        EventManager.ins.on(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this);

        this.nextLvInfo = LevelManager.ins.getNextLvInfo();
        if (LevelManager.ins.isLiveing) {
            this.nextLvInfo = LevelManager.ins.getNextLiveLvInfo();
        }

        this.updateShareBtn();
        this.updateNextBtn();
        this.initAddBtn();
        if (AdManager.gameType == GAME_TYPE_ENUM.WO_YAO_BIAN_TU_LE) {

        } else {
            this.showAddShortcutUI();
        }

        let idx = LevelManager.ins.getLvInfoIdx(LevelManager.ins.curLvInfo.type, LevelManager.ins.curLvInfo.lv) + 1;
        AdManager.reportLevel(idx, LevelManager.ins.curLvInfo.name, 1);
        AdManager.exitLv(LevelManager.ins.curLvInfo.name);

        if (AdManager.gameType != GAME_TYPE_ENUM.NAO_DONG_TIAN_HUA_BAN && AdManager.releaseType === RELEASE_TYPE_ENUM.bytedance && !AdManager.fuFangFinished()) {
            UIManager.ins.showPop('douyinPropUI', BUNDLE_TYPE_ENUM.HOME);
        }

        EventManager.ins.on(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN, this.hideAddBtn, this);

    }
    protected onDestroy(): void {
        EventManager.ins.off(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this);
        EventManager.ins.off(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN, this.hideAddBtn, this);

    }
    start() { }
    showAddShortcutUI() {

        let show = GameManager.ins.canShowAddShortcutUI();
        if (!show) return;
        // 每天弹一次
        /** 打开提示添加桌面 */
        AdManager.isShowAddShortcutBtn((active: boolean) => {
            if (active) UIManager.ins.showPop('addShortcutUI', BUNDLE_TYPE_ENUM.HOME);
            GameManager.ins.saveAddShortcutTime();
        });
    }
    updateShareBtn() {
        this.shareBtn.active = AdManager.isShowShareBtn();
    }
    updateNextBtn() {
        this.nextBtn.active = !!this.nextLvInfo;
    }
    updateWinInfo(winInfo: string) {
        if (winInfo)
            this.winLab.string = winInfo;
    }
    initAddBtn() {
        this.addBtn.active = false;
        // AdManager.isShowAddShortcutBtn((active: boolean) => {
        //     this.addBtn.active = active;
        // });
    }
    hideAddBtn() {
        this.addBtn.active = false;
    }

    updateIQ() {
        const gameData = StorageUtil.instance.getGameData();
        gameData.brain++;
        Message.instance.send(EventID.REFRESH_TOP_BAR, { type: EResType.IQ, value: 5 });
    }

    //存储当前胜利的关卡
    ceshiWinPassLv() {
        let lvName = LevelManager.ins.getBundleName();
        let story = Tools.getStorage('PassLv_1', true);
        if (Tools.getStorage('PassLv_1') == null) {
            let pass = []
            pass.push(lvName)
            Tools.setStorage("PassLv_1", pass, true);
            // 更新游戏中iq
            this.updateIQ();
        } else {
            let pass1 = story;
            pass1.push(lvName)
            Tools.setStorage("PassLv_1", pass1, true);
        }
    }
    openUI(winInfo: string) {
        cc.log("WinUI openUI")
        this.updateWinInfo(winInfo);
        AdManager.stopVideoScreen();
        //通过本关
        LevelManager.ins.finishCurrentLevel();
        //解锁下一关
        LevelManager.ins.unlockNextLevel();
        EventManager.ins.off(EVENT_NAME_ENUM.OPEN_UI, this.openUI, this);
    }


    clickBtnReturn() {
        var isWinReGameBtnBendi = Tools.getStorage('isWinReGameBtn')
        if (isWinReGameBtnBendi) {
            return
        }
        AudioManager1.playClick();
        UIManager.ins.closeAll();
        UIManager.ins.showUI("LevelUI", BUNDLE_TYPE_ENUM.HOME);
    }

    clickBtnNext() {
        var isWinReGameBtnBendi = Tools.getStorage('isWinReGameBtn')
        if (isWinReGameBtnBendi) {
            return
        }
        AudioManager1.playClick();
        UIManager.ins.closeAll();
        LevelManager.ins.loadLevelPre(this.nextLvInfo);
        if (AdManager.gameType == GAME_TYPE_ENUM.WO_YAO_BIAN_TU_LE) {
            if (GameManager.ins.canShowSubscribe()) {
                AdManager.addSubscribe(() => {

                });
            }
        }
    }

    clickBtnShare() {
        var isWinReGameBtnBendi = Tools.getStorage('isWinReGameBtn')
        if (isWinReGameBtnBendi) {
            return
        }
        AudioManager1.playClick();
        AdManager.shareVideoScreen();
    }

    /** 点击添加桌面 */
    clickBtnAddShortCut() {
        AudioManager1.playClick();
        // UIManager.ins.showPop('addShortcutUI', BUNDLE_TYPE_ENUM.HOME);
        AdManager.addTable(() => EventManager.ins.emit(EVENT_NAME_ENUM.HIDE_ADD_SHORTCUT_BTN));
    }

}
